package com.rabbitmq.produce.retrun;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

import static com.rabbitmq.produce.callback.ConfirmCallbackConfig.messageMap;

/**
 * @author zhangbowen
 * @date 2019/3/20
 **/
@Component
public class RetrunCallBack {
    //implements RabbitTemplate.ReturnCallback {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /*  @PostConstruct
      private void retrun() {
          rabbitTemplate.setReturnCallback(this);
      }

      @Override
      public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
          System.out.println(message);
          System.out.println(replyCode);
          System.out.println(replyText);
          System.out.println(exchange);
          System.out.println(routingKey);
          messageMap.remove(message.getMessageProperties().getMessageId());
      }*/
    @PostConstruct
    private void retrun() {
        rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
            System.out.println(message);
            System.out.println(replyCode);
            System.out.println(replyText);
            System.out.println(exchange);
            System.out.println(routingKey);
            messageMap.remove(message.getMessageProperties().getMessageId());
        });
    }
}
